package com.wanmait.recruitment.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.recruitment.pojo.*;
import com.wanmait.recruitment.service.*;

import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("manage/company")
public class CompanyListController {
    @Resource
    private CompanyService companyService;


    @Resource
    private ConfigService configService;
    @Resource
    private CityService cityService;
    @Resource
    private IndustryService industryService;

    @Resource
    private StageService stageService;
    @Resource
    private  CompanyTagService companyTagService;

    @RequestMapping("list")
    public  ModelAndView list(){
        ModelAndView modelAndView=new ModelAndView();
        List<City>cityList=cityService.selectList();
        List<Industry>industryList=industryService.findAlls();
        List<Stage>stageList=stageService.findAll();
        modelAndView.addObject("cityList",cityList);
        modelAndView.addObject("industryList",industryList);
        modelAndView.addObject("stageList",stageList);
        return  modelAndView;



    }
    @RequestMapping("listdata")
    public ModelAndView listdata(HttpServletRequest request,Integer cityId,Integer industryId,Integer stageId,String companyName,Integer pageNum){
        ModelAndView modelAndView =new ModelAndView();
        //页码
        //int pageNum;
//        try {
//            pageNum=Integer.parseInt(request.getParameter("pageNum"));
//        } catch (NumberFormatException e) {
//            pageNum=1;
//        }
        //每页显示条数
        int pageSize;
        try {
            pageSize= Integer.parseInt(configService.getValueByKey("company_count"));
        } catch (NumberFormatException e) {
            pageSize=Integer.parseInt(configService.getDefaultValueByKey("company_count"));
        }
        PageInfo<Company>pageInfo=companyService.findPageManage(pageNum,pageSize,cityId,industryId,stageId, Strings.isEmpty(companyName)?null:companyName);

        modelAndView.addObject("pageInfo",pageInfo);

        return  modelAndView;

    }
    @RequestMapping("delete")
    @ResponseBody
    public void delete(Integer id){

        companyService.delete(id);

    }
    @RequestMapping("batchDelete")
    @ResponseBody
    public  void batchDelete(String ids){
       companyService.delete(ids);

    }
    @RequestMapping("companytdetails")
    public  ModelAndView companytdetails(Integer id){
        ModelAndView modelAndView=new ModelAndView();
        Company company=companyService.FindAllCompany(id);
        List<CompanyTag>companyTagList=companyTagService.selectById(id);
        modelAndView.addObject("company",company);
        modelAndView.addObject("companyTagList",companyTagList);
        return  modelAndView;

    }
    @RequestMapping("updateHot")
    @ResponseBody
    public  void updateHot(Integer id){
          companyService.upudateAuthable(id);
    }
    @RequestMapping("updateOpened")
    @ResponseBody
    public  void updateOpened(Integer id){
       companyService.upudateOpened(id);
    }


}
